#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUF_SIZE 64
#define NO -1
#define NAME_SIZE 64

typedef struct filenode{
    char name[NAME_SIZE];
    long offset;
}filenode,*filelist;

void my_close(int sockfd,int fd_r){

    if(sockfd > 0){
        close(sockfd);
    }
    
    if(fd_r > 0){
        close(fd_r);
    }

    return;
}


int my_send(int sockfd,void* buf,int length){

    int temp=0;
    int ftemp=0;

    while(1){
        
        ftemp=send(sockfd,buf+temp,length - temp,0);//0 阻塞(发送的缓冲区满/网络阻塞);
        if(ftemp == -1){
            perror("my_send err");
            return -1;
        }
        temp+=ftemp;
        if(temp >= length){
            break;
        }
        
    }
    return 0;
}

int my_recv(int sockfd,void* buf,int length){

    int temp=0;
    int ftemp=0;

    while(1){
        
        ftemp=recv(sockfd,buf+temp,length - temp,0);//0 阻塞(发送的缓冲区满/网络阻塞);
        if(ftemp == -1){
            perror("my_send err");
            return -1;
        }
        temp+=ftemp;
        if(ftemp == 0){
            return temp;
        }
        if(temp >= length){
            break;
        }
        
    }
    return temp;
}

long file_size(char* filename){
    long temp =0;
    if(access(filename,F_OK) == 0){
        int fd_r = open(filename,O_RDONLY);
        if(fd_r == 0){
            perror("open err");
            return -1;
        }
        temp=lseek(fd_r,0,SEEK_END);

        close(fd_r);
    }else{
        return 0;
    }
    

    return temp;
}


int main(int argc,char const *argv[]){
    if(argc < 2){
        printf("usage err\n");
        return -1;
    }
    int sockfd = socket(AF_INET,SOCK_STREAM,0);//ipv4,tcp
    if(sockfd == -1){
        perror("sock err");
        return -1;
    }
    //服务器地址:IP 端口号
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    // server_addr.sin_port =htons(atoi(argv[2]));
    server_addr.sin_port =htons(8888);

    // server_addr.sin_addr.s_addr=inet_addr(argv[1]);
    server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");

    printf("IP=%s\n",inet_ntoa(server_addr.sin_addr));
    socklen_t addrlen= sizeof(server_addr);
    

    //连接
    if(connect(sockfd,(struct sockaddr*)&server_addr,addrlen) < 0){
        perror("coonet err");
        my_close(sockfd,NO);
        return -1;
    }
    printf("connet  success\n");

   //发送文件信息
    filenode stat;
    memset(&stat,0,sizeof(stat));
    strcpy(stat.name,argv[1]);
    long size_file=0;
    stat.offset=file_size(stat.name);
    printf("%ld\n",stat.offset);
    if(stat.offset < 0){
        perror("read size err");
        my_close(sockfd,NO);
        return -1;
    }else{
        stat.offset=htonl(stat.offset);
    }

    if(my_send(sockfd,&stat,72) == -1){ //send 不一定会一次全部发送完，分多次发送完
        perror("send err");
        my_close(sockfd,NO);
        return -1;
    }
    if(my_recv(sockfd,&size_file,sizeof(long)) < 0){
        my_close(sockfd,NO);
        return -1;
    }
    printf("%ld\n",size_file);
    size_file=ntohl(size_file) - ntohl(stat.offset);
    if(size_file == 0){
        printf("recv complete\n");
        my_close(sockfd,NO);
        return 0;
    }

    //收发数据
    int fd_w = open(stat.name,O_WRONLY | O_CREAT | O_TRUNC,0666);
    if(fd_w == -1){
        perror("open err");
        my_close(sockfd,NO);
        return -1;
    }
    lseek(fd_w,stat.offset,SEEK_SET);
    char buf[BUF_SIZE]={0};
    int ret=0;
    while(1){
        ret =my_recv(sockfd,buf,sizeof(buf));
        if(ret < 0){
            perror("recv err");
            continue;
        }else if(ret > 0){
            size_file-=ret;
            if(write(fd_w,buf,ret) == -1){
                break;
            }
            if(ret < BUF_SIZE){
                printf("recv complete\n");
                break;
            }
            // printf("recv data:%s\n",buf);
        }else{
            printf("perr quit\n");
            break;
        }
    }


    //关闭
    my_close(sockfd,fd_w);


    return 0;
}
